package com.company;

import sun.misc.BASE64Decoder;

import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

public class Test {
    public static void main(String[] args) throws IOException {
        HashMap<String,String> map=new HashMap<>();
        map.put("pushdata","{\"type\":4,\"data\":{\"accid\":\"test13051787862\",\"liveid\":85,\"roomid\":\"67497660\",\"anchor_id\":\"189\"}");
        System.out.println(map);
//        String json= "{\"circle_num\":\"1\",\"taskId\":\"2\",\"time\":\"1576036976\",\"vid\":\"\"}";
//        String key= "qTkQDfzsSA9yTUEj";
//        System.out.println(XORUtil.encrypt(json.getBytes(),key.getBytes()));
//        try {
//            String res = new String(XORUtil.encrypt(json.getBytes(),key.getBytes()),"UTF-8");
//            System.out.println(res);
//        } catch (UnsupportedEncodingException e) {
//
//            // TODO Auto-generated catch block
//
//            e.printStackTrace();
//
//        }

//       String s =Utils.getTypeName("050302");
//       System.out.println(s);
        ;
//        System.out.println(URLEncoder.encode("（*ﾟ∀ﾟ）つ―{}@{}@{}-"));
//        getTimeInfo("1564755499");
//        File directory = new File("C:\\Users\\user3\\Desktop\\搬走.lrc");
////        converfile(directory.getPath());
//        String input = "C:\\Users\\user3\\Desktop\\15619570438784200.lrc";
//        String output = "C:\\Users\\user3\\Desktop\\156195704387842222.lrc";
//        saveAsUTF8(input, output);
//       String ss= readFile(directory.getPath());
//       byte[]s= gbk2utf8(ss);
//        String s1 =new String(s);
//        System.out.println(s1);

//        try {
//            System.out.println(codeString("C:\\Users\\user3\\Desktop\\搬走.lrc"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

//        String content= "【【帮我砍价】这是我在C嗨上看到的妩媚价廉的商品，只要看到0元就能免费拿】，椱ァ製这句话¢bGF1bmNoX2lkOjIsQ2hvcDox¢后咑閞\uD83D\uDC49C嗨\uD83D\uDC48";
//
//        String contentSub= content.substring(content.indexOf("¢")+1,content.indexOf("¢后"));
//
//        System.out.println(contentSub);
//
//        BASE64Decoder decoder = new BASE64Decoder();
//        byte[] bytes = decoder.decodeBuffer(contentSub);
//        System.out.println("BASE64解密：" + new String(bytes));

    }

    /**
     * 判断文件的编码格式
     *
     * @param fileName :file
     * @return 文件编码格式
     * @throws Exception
     */
    public static String codeString(String fileName) throws Exception {
        BufferedInputStream bin = new BufferedInputStream(
                new FileInputStream(fileName));
        int p = (bin.read() << 8) + bin.read();
        String code = null;


        switch (p) {
            case 0xefbb:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16BE";
                break;
            default:
                code = "GBK";
        }

        return code;
    }


    public static String converfile(String filepath) {
        System.out.println("ConvertFileCode---------&gt;" + filepath);
        File file = new File(filepath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        BufferedReader reader = null;
        String text = "";
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            bis.mark(4);
            byte[] first3bytes = new byte[3];
//   System.out.println("");
            //找到文档的前三个字节并自动判断文档类型。
            bis.read(first3bytes);
            System.out.println("convert" + first3bytes[0] + "==" + first3bytes[1] + "==" + first3bytes[2]);

            //5B,74,69
            bis.reset();
            if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB
                    && first3bytes[2] == (byte) 0xBF) {// utf-8
                reader = new BufferedReader(new InputStreamReader(bis, "utf-8"));
            } else if (first3bytes[0] == (byte) 0xFF
                    && first3bytes[1] == (byte) 0xFE) {
                reader = new BufferedReader(
                        new InputStreamReader(bis, "unicode"));
            } else if (first3bytes[0] == (byte) 0xFE
                    && first3bytes[1] == (byte) 0xFF) {
                reader = new BufferedReader(new InputStreamReader(bis,
                        "utf-16be"));
            } else if (first3bytes[0] == (byte) 0xFF
                    && first3bytes[1] == (byte) 0xFF) {
                reader = new BufferedReader(new InputStreamReader(bis,
                        "utf-16le"));
            } else {
                reader = new BufferedReader(new InputStreamReader(bis, "gbk"));
            }
            String str = reader.readLine();

            while (str != null) {
                text = text + str;
                str = reader.readLine();
            }
            System.out.println("text" + text);
            byte[] bytes = getUTF8Bytes(text);
            String name = new String(bytes);
            System.out.println(name);
//            String strUTF8 = URLDecoder.decode(text, "gbk");


//            System.out.println("newText"+strUTF8);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return text;

    }

    public static String readFile(String filePathAndName) {
        String fileContent = "";
        try {
            File f = new File(filePathAndName);
            if (f.isFile() && f.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(f), "gbk");
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
                    fileContent += line;
                }
                read.close();
            }
        } catch (Exception e) {
            System.out.println("读取文件内容操作出错");
            e.printStackTrace();
        }
        System.out.println(fileContent);
        return fileContent;
    }

    public static byte[] gbk2utf8(String chenese) {
        char c[] = chenese.toCharArray();
        byte[] fullByte = new byte[3 * c.length];
        for (int i = 0; i < c.length; i++) {
            int m = (int) c[i];
            String word = Integer.toBinaryString(m);
            System.out.println(word);

            StringBuffer sb = new StringBuffer();
            int len = 16 - word.length();
//            补零
            for (int j = 0; j < len; j++) {
                sb.append("0");
            }
            sb.append(word);
            sb.insert(0, "1110");
            sb.insert(8, "10");
            sb.insert(16, "10");
            System.out.println(sb.toString());
            String s1 = sb.substring(0, 8);
            String s2 = sb.substring(8, 16);
            String s3 = sb.substring(16);
            byte b0 = Integer.valueOf(s1, 2).byteValue();
            byte b1 = Integer.valueOf(s2, 2).byteValue();
            byte b2 = Integer.valueOf(s3, 2).byteValue();
            byte[] bf = new byte[3];
            bf[0] = b0;
            fullByte[i * 3] = bf[0];
            bf[1] = b1;
            fullByte[i * 3 + 1] = bf[1];
            bf[2] = b2;
            fullByte[i * 3 + 2] = bf[2];
        }
        return fullByte;
    }

    public static byte[] getUTF8Bytes(String name) {
        int n = name.length();
        byte[] utfBytes = new byte[3 * n];
        int k = 0;
        for (int i = 0; i < n; i++) {
            int m = name.charAt(i);
            if (m < 128 && m >= 0) {
                utfBytes[k++] = (byte) m;
                continue;
            }
            utfBytes[k++] = (byte) (0xe0 | (m >> 12));
            utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
            utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
        }
        if (k < utfBytes.length) {
            byte[] tmp = new byte[k];
            System.arraycopy(utfBytes, 0, tmp, 0, k);
            return tmp;
        }
        return utfBytes;
    }


    public static void saveAsUTF8(String inputFileUrl, String outputFileUrl) throws IOException {
        String inputFileEncode = EncodingDetect.getJavaEncode(inputFileUrl);
        System.out.println("inputFileEncode===" + inputFileEncode);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(new FileInputStream(inputFileUrl), inputFileEncode));
        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(outputFileUrl), "UTF-8"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line + "\r\n");
        }
        bufferedWriter.close();
        bufferedReader.close();
        String outputFileEncode = EncodingDetect.getJavaEncode(outputFileUrl);
        System.out.println("outputFileEncode===" + outputFileEncode);
        System.out.println("txt文件格式转换完成");
    }

    public static String getItemTimeInfo(String timeSpa){
        try{
            String createDate ;
            long timeSpaLong =Long.valueOf(timeSpa);
            long differenceValue =System.currentTimeMillis()/1000 - timeSpaLong;
            System.out.println(differenceValue);
            if(differenceValue < 3600){
                if((differenceValue  / 60 )==0){
                    createDate = "刚刚";
                }else{
                    createDate = (differenceValue / 60 ) + "分钟前";
                }
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                if (sdf.format(new Date(timeSpaLong * 1000)).equals(Calendar.getInstance().get(Calendar.YEAR) + "")) {//当年
                    SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd HH:mm");
                    createDate = sdf2.format(new Date(timeSpaLong * 1000));
                } else {
                    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                    createDate = sdf3.format(new Date(timeSpaLong * 1000));
                }
            }
            System.out.println(createDate);
            return createDate;
        }catch (Exception e){
            return "";
        }

    }


}
